package com.atshiyou.controller;

import com.atshiyou.bean.BillType;
import com.atshiyou.bean.Bills;
import com.atshiyou.service.BillTypeService;
import com.atshiyou.service.BillsService;
import com.atshiyou.util.PageUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;

@Controller
public class BillsController {
    @Resource
    private BillTypeService billTypeService;
    @Resource
    private BillsService billsService;


    @RequestMapping("/deleteById")
    @Transactional
    public void deleteById(int bid, HttpServletResponse response){
        int i = billsService.deleteByPrimaryKey(bid);
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if(i>0){
            writer.print("<script>alert('删除成功');location.href='/gettypes'</script>");
            return;
        }else {
            writer.print("<script>alert('删除失败');location.href='/gettypes'</script>");
        }

    }

    //修改
    @RequestMapping("/updateBill")
    @Transactional
    public String updateBill(Bills bills){
        int i = billsService.updateByPrimaryKey(bills);
        if(i>0){
            return "redirect:/gettypes";
        }else{
            return "redirect:/findById?bid="+bills.getId();
        }

    }


    @RequestMapping("/findById")
    public String findById(int bid,ModelMap modelMap){
        Bills bills = billsService.selectByPrimaryKey(bid);
        modelMap.addAttribute("bills",bills);
        List<BillType> types = billTypeService.getTypes();
        modelMap.addAttribute("types",types);
        return "update";
    }

    @RequestMapping("/insertBill")
    @Transactional
    public String insert(Bills bills, HttpServletRequest request){ //接收前端传来的参数：使用对象接收，要求页面的名称和属性名一致
        try {
            request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        System.out.println("bills-------------"+bills);
        int i = billsService.insert(bills);
        if(i==1){
            return "redirect:/gettypes"; //回到主页面
        }else{
            return "add";//回到新增页面
        }
    }

    @RequestMapping("/getBillType")
    public String getBillType(ModelMap modelmap){
        List<BillType> types = billTypeService.getTypes();
        modelmap.addAttribute("types",types);
        return "add";

    }

    @RequestMapping("/gettypes")
    public String getTypes(ModelMap map){
        //查询所有的账单
        PageInfo<Bills> info = billsService.findAll(-1,null,null,1,PageUtil.PAGESIZE);
        //查询账单的所有类型
        List<BillType> billTypeList = billTypeService.getTypes();
        //保存数据给前台
        map.addAttribute("info",info);
        map.addAttribute("billTypeList",billTypeList);
        return "show";
    }

    @RequestMapping("/getAllBills")
    public String getBills(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "-1") int typeid, String begin, String end, ModelMap map){
        PageInfo<Bills> info = billsService.findAll(typeid, begin, end,page, PageUtil.PAGESIZE);
        //查询账单的所有类型
        List<BillType> billTypeList = billTypeService.getTypes();
        map.addAttribute("info",info);
        //数据回显
        map.addAttribute("billTypeList",billTypeList);
        map.addAttribute("tid",typeid);
        map.addAttribute("begin",begin);
        map.addAttribute("end",end);

        return "show";
    }
}
